import { Behaviour, getGameObjectById } from "../gameengine";
import { Rect_Collider, RectColliderType } from "./rect_Collider";
import { MainRoleBehaviour } from "../mainRoleBehaviour";


export class ColliderSystem extends Behaviour {


    rectColliders: Rect_Collider[] = [];

    itemColliders: Rect_Collider[] = [];

    public addRectCollider(rectCollider: Rect_Collider) {
        this.rectColliders.push(rectCollider);
        console.log('添加了一个矩形碰撞盒')
    }

    public removeRectCollider(rectCollider: Rect_Collider) {
        const index = this.rectColliders.indexOf(rectCollider);
        if (index != -1) {
            this.rectColliders.splice(index, 1);
            console.log('移除了一个矩形碰撞盒');
        }
    }

    protected onStart() {
        console.log('碰撞系统启动')
    }

    public check(rectA: { left: number, right: number, top: number, bottom: number }, type: RectColliderType) {
        for (let rectB of this.rectColliders) {
            if (rectB.type != type) {
                continue;   //如果类型不对则什么都不做
            }
            if (rectA.right > rectB.left &&
                rectA.left < rectB.right &&
                rectA.bottom > rectB.top &&
                rectA.top < rectB.bottom
            ) {
                if (type == 2) {
                    if (this.itemColliders.length < 1) {
                        this.itemColliders.push(rectB);
                        console.log((this.itemColliders));
                    }
                    else {
                        for (let itemCollider of this.itemColliders) {
                            if (itemCollider != rectB) {
                                this.itemColliders.push(rectB);
                                console.log((this.itemColliders));
                            }
                        }
                    }
                }
                // console.log(this.itemColliders);
                return true;
            }
        }
        return false;
    }


    protected onUpdate(advancedTime: number) {
        if (this.itemColliders.length < 1) {
            console.log('itemColliders为空');
            return;
        }

        // console.log(this.rectColliders);
        const rectA = getGameObjectById("mainRole").getScript(Rect_Collider);
        const rectB = this.itemColliders[0];
        // console.log(rectB);

        const speed = getGameObjectById("mainRole").getScript(MainRoleBehaviour).SPEED;
        if (rectA.right + speed > rectB.left &&
            rectA.left - speed < rectB.right &&
            rectA.bottom + speed > rectB.top &&
            rectA.top - speed < rectB.bottom
        ) {
            console.log('碰撞');
            // if (rectA.onColliderStart) {
            //     rectA.onColliderStart();
            // }
            // if (rectB.onColliderStart) {
            //     rectB.onColliderStart();
            // }
        } else {
            // console.log('未碰撞');
            for (let itemCollider of this.itemColliders) {
                this.itemColliders.pop();
            }

        }
    }
}